package router

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"week41/client/client"
	"week41/client/request"
	user "week41/common/proto"
)

func Login(c *gin.Context) {
	var data request.UserReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	login, err := client.Login(c, &user.LoginReq{
		Name:     data.Name,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"Code": 500,
			"Msg":  "登录失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "登录成功",
		"Data": login,
	})
}

func Register(c *gin.Context) {
	var data request.UserReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	login, err := client.Register(c, &user.RegisterReq{
		Name:     data.Name,
		Password: data.Password,
	})
	if err != nil {
		fmt.Println(login)
		c.JSON(http.StatusInternalServerError, gin.H{
			"Code": 500,
			"Msg":  "注册失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "注册成功",
		"Data": 3,
	})
}

func List(c *gin.Context) {
	login, err := client.List(c, &user.ListReq{})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"Code": 500,
			"Msg":  "展示失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "展示成功",
		"Data": login.GetList(),
	})
}
